package com.pituruh.utils;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class FindRouteXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static FindRouteSitesList sitesList = null;
	RouteResultList routeResult = null;
	NodeHelpers nodeHelper = null;

	public static FindRouteSitesList getSitesList() {
		return sitesList;
	}

	public static void setSitesList(FindRouteSitesList sitesList) {
		FindRouteXMLHandler.sitesList = sitesList;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("TJRoute")) {
			/** Start */
			sitesList = new FindRouteSitesList();
		}
		else if (localName.equals("FastestRoutes")) {
			routeResult = new RouteResultList();
		}
		else if (localName.equals("NodeHelper")) {
			nodeHelper = new NodeHelpers();
		}

	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("StartShelter"))
			sitesList.setStartShelter(currentValue);
		else if (localName.equalsIgnoreCase("StopShelter"))
			sitesList.setStopShelter(currentValue);
		else if (localName.equalsIgnoreCase("DateSearch"))
			sitesList.setDateSearch(currentValue);
		else if (localName.equalsIgnoreCase("Method"))
			sitesList.setFindMethod(Integer.parseInt(currentValue));
		else if (localName.equalsIgnoreCase("name"))
			routeResult.setName(currentValue);
		else if (localName.equalsIgnoreCase("coordinate"))
			routeResult.setCoordinate(currentValue);
		else if (localName.equalsIgnoreCase("distance"))
			routeResult.setDistance(Float.parseFloat(currentValue));
		else if (localName.equalsIgnoreCase("duration"))
			routeResult.setDuration(Float.parseFloat(currentValue));
		else if (localName.equalsIgnoreCase("path"))
			routeResult.setPath(currentValue);
		else if (localName.equalsIgnoreCase("time"))
			routeResult.setTime(currentValue);
		else if (localName.equalsIgnoreCase("node"))
			nodeHelper.setNodes(currentValue);
		else if (localName.equalsIgnoreCase("NodeHelper"))
			routeResult.setNodeHelper(nodeHelper);
		else if (localName.equalsIgnoreCase("FastestRoutes"))
			sitesList.setRoutes(routeResult);

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}